<template>
  <div class="dialog-form">
    <el-dialog v-model="formVisible" class="elDialogBox" title="填写处理结果" @close="cancel">
      <div class="dialogCenter">
        <el-form ref="queryRef" :model="formData" :rules="rules" label-width="108px">
          <el-form-item label="处理时间：" prop="processingTime">
            <el-date-picker v-model="formData.processingTime" allow-input class="wt-400" clearable enable-time-picker
                            placeholder="请选择"/>
          </el-form-item>
          <el-form-item label="处理结果：" prop="processingResult">
            <el-input v-model="formData.processingResult" :maxlength="100" class="wt-400" placeholder="请输入"
                      resize="none" show-word-limit type="textarea"/>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, watch} from 'vue';

const {proxy} = getCurrentInstance();
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(['handleClose', 'handleSub']);
const resetType = ref('empty');
const form = ref();
const formVisible = ref(false);
const formData = ref({});

const rules = {
  processingTime: [
    {
      required: true,
      message: '处理时间为空，请选择处理时间',
      trigger: 'change',
    },
  ],
  processingResult: [
    {
      required: true,
      message: '处理结果为空，请输入处理结果',
      trigger: 'blur',
    },
  ],
};

watch(
    () => props.visible,
    (val) => {
      formVisible.value = val;
    }
);

// 提交表单
const submitForm = () => {
  proxy.$refs['queryRef'].validate((valid) => {
    if (valid) {
      emit('handleSub', formData.value);
      handleClear()
    }
  });
};

// 清除表单数据
const handleClear = () => {
  proxy.resetForm('queryRef');
};

// 点击取消关闭
const cancel = () => {
  handleClear();
  emit('handleClose');
};

defineExpose({
  handleClear,
});
</script>
